<?php


namespace app\common\tool;


use Firebase\JWT\JWT;

class Token
{
    private static $config = [
        "iss"  => "http://blog.hideyoshi.top",
        "aud"  => "http://blog.hideyoshi.top",
        "iat"  => 1356999524,
        "nbf"  => 1357000000,
        'exp'  => 0,
        'info' => []
    ];

    /**
     * @param array $config
     */
    public static function setConfig(array $config)
    {
        self::$config = $config;
    }

    /**
     * @param array $info
     * @param string $key
     * @return string
     */
    public static function encode(array $info, string $key = 'u know,i dont know'): string
    {
        self::$config['info'] = $info;
        if (!self::$config['exp']) unset(self::$config['exp']);
        return JWT::encode(self::$config, $key);
    }

    /**
     * @param string $token
     * @param string $key
     * @return array
     */
    public static function decode(string $token, string $key = 'u know,i dont know'): array
    {
        return (array)JWT::decode($token, $key, ['HS256']);
    }
}